import type { App } from 'vue'
import type { IAppConfig } from './manager/app'
import type { Theme } from './manager/theme'
import type { RouterConfig, TUtil } from './utils/_index'

import useUi from './layouts/useUi'
import { initAppConfig } from './manager/app'
import { initTheme } from './manager/theme'
import { $util as _util } from './utils/_index'
import registerVueTools from './vue-tools'

export { default as useAppStore } from './manager/app'
export { default as globalLifeCycle } from './manager/life-cycle'
export { default as useThemeStore } from './manager/theme'
export { useUi }

declare global {
  interface Uni {
    $PAGE_PATH: string
    $util: TUtil
  }
}

uni.$util = _util as TUtil

interface TConfig {
  useAppConfig?: boolean
  appConfig?: IAppConfig
  routerConfig?: RouterConfig
  useThemeConfig?: boolean
  themeConfig?: Theme
  popupConfig?: {
    useSys?: boolean
    config: {
      confirmColor?: string
      cancelColor?: string
    }
  }
}
export default function bootstrap(app: App<unknown>, { routerConfig, useAppConfig = true, appConfig, useThemeConfig = true, themeConfig, popupConfig }: TConfig = {}) {
  _util.setRouter(routerConfig)
  useAppConfig && initAppConfig(appConfig)
  useThemeConfig && initTheme(themeConfig)
  if (popupConfig) {
    const ui = useUi()
    Object.assign(ui.popupConfig, popupConfig.config, { useSys: popupConfig.useSys })
  }
  registerVueTools(app)
}
